package com.wmeimob.fastboot.jiubei.service;

import com.wmeimob.fastboot.core.exception.CustomException;
import com.wmeimob.fastboot.jiubei.entity.ShopGoodsClassify;
import com.wmeimob.fastboot.jiubei.mapper.ShopGoodsClassifyMapper;
import com.wmeimob.fastboot.util.InputValidator;
import com.wmeimob.fastboot.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static com.wmeimob.fastboot.util.InputValidator.checkLengthRange;

/**
 * @version 1.0
 * @ClassName ShopGoodsClassifyServiceImpl
 * @Description 商城商品分类 服务类实现
 * @Author wjt
 * @Date Wed Aug 26 21:50:57 CST 2020
 **/

@Service
@Transactional(rollbackFor = {Exception.class})
@Slf4j
public class ShopGoodsClassifyServiceImpl implements ShopGoodsClassifyService {

    @Resource
    private ShopGoodsClassifyMapper shopGoodsClassifyMapper;


    @Override
    public List<ShopGoodsClassify> list(ShopGoodsClassify shopGoodsClassify) {
        Example example = new Example(ShopGoodsClassify.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(shopGoodsClassify.getId())) {
            criteria.andEqualTo("id", shopGoodsClassify.getId());
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getPid())) {
            criteria.andEqualTo("pid", shopGoodsClassify.getPid());
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getName())) {
            criteria.andLike("name", StringUtils.fullFuzzy(shopGoodsClassify.getName()));
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getPic())) {
            criteria.andLike("pic", StringUtils.fullFuzzy(shopGoodsClassify.getPic()));
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getSort())) {
            criteria.andEqualTo("sort", shopGoodsClassify.getSort());
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getDel())) {
            criteria.andEqualTo("del", shopGoodsClassify.getDel());
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getGmtCreate())) {
            criteria.andEqualTo("gmtCreate", shopGoodsClassify.getGmtCreate());
        }
        if (!StringUtils.isEmpty(shopGoodsClassify.getGmtModified())) {
            criteria.andEqualTo("gmtModified", shopGoodsClassify.getGmtModified());
        }
        criteria.andEqualTo("del", false);
        example.orderBy("id").desc();
        List<ShopGoodsClassify> shopGoodsClassifys = shopGoodsClassifyMapper.selectByExample(example);
        return shopGoodsClassifys;
    }

    @Override
    public ShopGoodsClassify queryShopGoodsClassifyById(Object id) {
        return shopGoodsClassifyMapper.selectByPrimaryKey(id);
    }


    @Override
    public void addShopGoodsClassify(ShopGoodsClassify shopGoodsClassify) {
        checkClassName(shopGoodsClassify);
        shopGoodsClassify.setGmtCreate(new Date());
        int result = shopGoodsClassifyMapper.insertSelective(shopGoodsClassify);
        if (result < 1) {
            throw new CustomException("新增失败");
        }
    }
    private void checkClassName(ShopGoodsClassify goodsClassify) {
        checkLengthRange(goodsClassify.getName(), 0, 20, "分类名称");
        InputValidator.checkNumber(1, 10000, goodsClassify.getSort(), "排序值");

        Example example = new Example(ShopGoodsClassify.class);
        example.createCriteria().andEqualTo("name", goodsClassify.getName()).andEqualTo("del", false).andNotEqualTo(
                "id", goodsClassify.getId());
        List<ShopGoodsClassify> goodsClassifyList = shopGoodsClassifyMapper.selectByExample(example);
        if (goodsClassifyList.size() > 0) {
            throw new CustomException("分类名称重复");
        }
    }
    @Override
    public void removeShopGoodsClassify(Object id) {
        ShopGoodsClassify shopGoodsClassify = new ShopGoodsClassify();
        shopGoodsClassify.setId(Integer.parseInt(id.toString()));
        shopGoodsClassify.setDel(true);
        int result = shopGoodsClassifyMapper.updateByPrimaryKeySelective(shopGoodsClassify);
        if (result < 1) {
            throw new CustomException("删除失败");
        }
    }

    @Override
    public void modifyShopGoodsClassify(ShopGoodsClassify shopGoodsClassify) {
        checkClassName(shopGoodsClassify);
        shopGoodsClassify.setGmtModified(new Date());
        int result = shopGoodsClassifyMapper.updateByPrimaryKeySelective(shopGoodsClassify);
        if (result < 1) {
            throw new CustomException("编辑失败");
        }
    }

    @Override
    public List<ShopGoodsClassify> findAll(ShopGoodsClassify shopGoodsClassify) {
        return shopGoodsClassifyMapper.findAll(shopGoodsClassify);
    }

    @Override
    public List<ShopGoodsClassify> selectListByPidFirst() {
        Example example = new Example(ShopGoodsClassify.class);
        example.orderBy("sort").desc();
        example.createCriteria().andEqualTo("pid", 0).andEqualTo("del", false);
        return shopGoodsClassifyMapper.selectByExample(example);
    }

    @Override
    public List<ShopGoodsClassify> selectListByPid(ShopGoodsClassify goodsClassify) {
        Example example = new Example(ShopGoodsClassify.class);
        example.orderBy("sort").desc();
        example.createCriteria().andEqualTo("pid", goodsClassify.getId()).andEqualTo("del", false);
        return shopGoodsClassifyMapper.selectByExample(example);
    }

}
